package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Category;
import com.itheima.bean.PageParams;
import com.itheima.bean.Setmeal;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.MyException;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


/**
 * @author hebing
 * @version 1.0
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * 添加
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R addSetmeal(@RequestBody SetmealDto setmealDto) {
        //调用service添加数据
        int rows = setMealService.addSetmeal(setmealDto);
        if (rows > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParams params) {
        try {
            //调用service进行分页查询
            IPage<SetmealDto> list = setMealService.findPage(params);
            return R.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteById(@RequestParam List<Long> ids) {
        //调用service层进行删除
        int rows = setMealService.deleteById(ids);
        if (rows > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 批量起售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R updateStatusToON(@RequestParam List<Long> ids) {
        //调用service进行起售
        int rows = setMealService.updateStatusToOn(ids);
        //判定
        if (rows > 0) {
            return R.success("批量操作成功");
        } else {
            throw new MyException("状态无需更改");
        }
    }


    /**
     * 批量停售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R updateStatusToOff(@RequestParam List<Long> ids) {
        //调用service进行停售
        int rows = setMealService.updateStatusToOff(ids);
        //判定
        if (rows > 0) {
            return R.success("批量操作成功");
        } else {
            throw new MyException("状态无需更改");
        }
    }

    /**
     * 根据categoryId和status查询数据库表Setmeal得到结果
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Long categoryId, Integer status) {
        try {
            //调用service进行查询
            List<Setmeal> setmealList = setMealService.findByCategoryId(categoryId, status);
            return R.success(setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }
}
